﻿<UserControl x:Class="tomenglertde.Wax.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800"
             xmlns:dgx="clr-namespace:DataGridExtensions;assembly=DataGridExtensions"
             xmlns:wax="urn:wax"
             xmlns:toms="urn:TomsToolbox"
             xmlns:local="clr-namespace:tomenglertde.Wax"
             Name="Wax"
             d:DataContext="{d:DesignInstance local:MainViewModel}">
  <!-- TODO:
    xmlns:vsfx="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.10.0"
    TextElement.Foreground="{DynamicResource {x:Static vsfx:VsBrushes.ToolWindowTextKey}}"
    Background="{DynamicResource {x:Static vsfx:VsBrushes.ToolWindowBackgroundKey}}"-->
  <UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <Style TargetType="Control" x:Key="SquareControlStyle">
      <Setter Property="Width" Value="{Binding Path=ActualHeight ,RelativeSource={RelativeSource Self}}" />
      <Setter Property="TextElement.FontWeight" Value="Bold" />
    </Style>
    <Style TargetType="DataGrid" x:Key="DefaultDataGridStyle">
      <Setter Property="HorizontalGridLinesBrush" Value="LightGray" />
      <Setter Property="VerticalGridLinesBrush" Value="LightGray" />
      <Setter Property="RowHeight" Value="22" />
      <Setter Property="VerticalContentAlignment" Value="Center" />
      <Setter Property="CanUserAddRows" Value="False" />
      <Setter Property="CanUserDeleteRows" Value="False" />
      <Setter Property="CanUserReorderColumns" Value="False" />
      <Setter Property="CanUserResizeRows" Value="False" />
      <Setter Property="AutoGenerateColumns" Value="False" />
    </Style>
    <Style TargetType="DataGrid" x:Key="DirectoryDataGridStyle" BasedOn="{StaticResource DefaultDataGridStyle}">
      <Setter Property="SelectionMode" Value="Single" />
      <Setter Property="RowStyle">
        <Setter.Value>
          <Style TargetType="DataGridRow">
            <Style.Triggers>
              <DataTrigger Binding="{Binding MappedNode}" Value="{x:Null}">
                <Setter Property="Background" Value="LightCoral" />
              </DataTrigger>
            </Style.Triggers>
          </Style>
        </Setter.Value>
      </Setter>
      <Style.Triggers>
        <DataTrigger Binding="{Binding InstallDirectoryMapping.MappedNode}" Value="{x:Null}">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
    <Style TargetType="ToolBarTray">
      <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.MenuBarBrushKey}}" />
    </Style>
    <Style TargetType="ToolBar">
      <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.MenuBarBrushKey}}" />
      <Setter Property="KeyboardNavigation.TabNavigation" Value="Once" />
      <Setter Property="Template">
        <!-- A simplified tool bar without overflow and thumb -->
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ToolBar}">
            <Grid Margin="3,1,1,1" SnapsToDevicePixels="true">
              <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                      Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                <Grid KeyboardNavigation.TabIndex="1" KeyboardNavigation.TabNavigation="Local">
                  <ToolBarPanel x:Name="PART_ToolBarPanel" IsItemsHost="true" Margin="0,1,2,2"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Grid>
              </Border>
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </UserControl.Resources>
  <DockPanel>
    <ToolBarTray DockPanel.Dock="Top" Margin="0">
      <ToolBar>
        <Button ToolTip="Refresh" Click="Refresh_Click">
          <Image SnapsToDevicePixels="True" Source="/Wax;component/Resources/refresh.png" />
        </Button>
        <Separator />
        <CheckBox Content="Deploy symbols" IsChecked="{Binding DeploySymbols}" />
        <Separator />
        <Button x:Name="LikeButton" ToolTip="Support the project by writing a short review."
                Tag="https://visualstudiogallery.msdn.microsoft.com/ab48d869-8f75-4e02-8e7d-577346065aa6/view/Reviews">
          <Image SnapsToDevicePixels="True" Source="/Wax;component/Resources/like.png" />
        </Button>
        <Button x:Name="HelpButton" ToolTip="Documentation"
                Tag="https://waxsetupeditor.codeplex.com/documentation">
          <Grid Width="16" Height="16">
            <TextBlock Text="?" FontWeight="Bold" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center" />
          </Grid>
        </Button>
      </ToolBar>
    </ToolBarTray>
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="4*" />
      </Grid.ColumnDefinitions>
      <DockPanel Grid.Column="0">
        <Grid DockPanel.Dock="Top">
          <local:GroupBox Ordinal="1" Header="Setup project to edit:">
            <ListBox x:Name="SetupProjectListBox"
                     ItemsSource="{Binding Solution.WixProjects}"
                     SelectedItem="{Binding SelectedWixProject}"
                     MinHeight="30" MaxHeight="100"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                     Loaded="SetupProjectListBox_Loaded">
              <ListBox.ItemTemplate>
                <DataTemplate DataType="wax:WixProject">
                  <TextBlock>
                    <Run Text="{Binding Name, Mode=OneWay}" FontWeight="Bold" Foreground="#202020" />
                    (<Run Text="{Binding RelativeFolder, Mode=OneWay}" />)
                  </TextBlock>
                </DataTemplate>
              </ListBox.ItemTemplate>
            </ListBox>
            <local:GroupBox.Style>
              <Style TargetType="local:GroupBox">
                <Setter Property="IsOk" Value="True" />
                <Style.Triggers>
                  <DataTrigger Binding="{Binding SelectedItem, ElementName=SetupProjectListBox}" Value="{x:Null}">
                    <Setter Property="IsOk" Value="False" />
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </local:GroupBox.Style>
          </local:GroupBox>
        </Grid>
        <Decorator Height="10" DockPanel.Dock="Top" />
        <Grid DockPanel.Dock="Top">
          <local:GroupBox Ordinal="2" Header="Root directory"
                          DataContext="{Binding InstallDirectoryMapping}"
                          VerticalAlignment="Top">
            <Grid Margin="1">
              <TextBlock VerticalAlignment="Center" Margin="3">
                <Run Text="{Binding MappedNode.Id, Mode=OneWay}" Foreground="#202020" FontWeight="Bold" />
                (<Run Text="{Binding MappedNode.Path, Mode=OneWay}" />)
              </TextBlock>
              <ComboBox ItemsSource="{Binding UnmappedNodes}"
                        SelectedItem="{Binding MappedNodeSetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <ComboBox.Style>
                  <Style TargetType="ComboBox">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Style.Triggers>
                      <DataTrigger Binding="{Binding MappedNode}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Visible" />
                      </DataTrigger>
                    </Style.Triggers>
                  </Style>
                </ComboBox.Style>
              </ComboBox>
            </Grid>
            <local:GroupBox.Style>
              <Style TargetType="local:GroupBox">
                <Setter Property="IsOk" Value="True" />
                <Style.Triggers>
                  <DataTrigger Binding="{Binding MappedNode}" Value="{x:Null}">
                    <Setter Property="IsOk" Value="False" />
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </local:GroupBox.Style>
          </local:GroupBox>
        </Grid>
        <Decorator Height="10" DockPanel.Dock="Top" />
        <Grid DockPanel.Dock="Bottom" Background="{x:Static SystemColors.ControlBrush}">
          <Button Margin="3" HorizontalAlignment="Left"
                  Background="Transparent" BorderBrush="Transparent"
                  BorderThickness="0" ToolTip="Support the project."
                  Tag="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=799WX673GPQM8">
            <Button.Style>
              <Style TargetType="Button">
                <Setter Property="Opacity" Value="0.15" />
                <Style.Triggers>
                  <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard Name="DonateMouseEnter">
                      <Storyboard Duration="0:0:0.5" Storyboard.TargetProperty="Opacity">
                        <DoubleAnimation To="1.0" />
                      </Storyboard>
                    </BeginStoryboard>
                  </EventTrigger>
                  <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard Name="DonateMouseLeave">
                      <Storyboard Duration="0:0:0.5" Storyboard.TargetProperty="Opacity">
                        <DoubleAnimation To="0.15" />
                      </Storyboard>
                    </BeginStoryboard>
                  </EventTrigger>
                </Style.Triggers>
              </Style>
            </Button.Style>
            <Image SnapsToDevicePixels="True" Stretch="None" Source="/Wax;component/Resources/btn_donate_SM.gif" />
          </Button>
        </Grid>
        <Grid>
          <local:GroupBox Ordinal="3" Header="Projects to install:">
            <DockPanel>
              <CheckBox x:Name="ShowAllProjects" DockPanel.Dock="Top" Content="Show all projects" Margin="0,5"
                        IsEnabled="{Binding CanHideReferencedProjects}">
                <CheckBox.Style>
                  <Style TargetType="CheckBox">
                    <Style.Triggers>
                      <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="IsChecked" Value="true" />
                      </Trigger>
                    </Style.Triggers>
                  </Style>
                </CheckBox.Style>
              </CheckBox>
              <ListBox x:Name="ProjectsListBox" SelectionMode="Extended"
                       toms:MultiSelectorExtensions.SelectionBinding="{Binding SelectedVSProjects}"
                       ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListBox.ItemTemplate>
                  <DataTemplate DataType="wax:Project">
                    <CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}">
                      <TextBlock>
                        <Run Text="{Binding Name, Mode=OneWay}" FontWeight="Bold" Foreground="#202020" />
                        (<Run Text="{Binding RelativeFolder, Mode=OneWay}" />)
                      </TextBlock>
                    </CheckBox>
                  </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.Style>
                  <Style TargetType="ListBox">
                    <Setter Property="ItemsSource" Value="{Binding Solution.TopLevelProjects}" />
                    <Style.Triggers>
                      <DataTrigger Binding="{Binding IsChecked, ElementName=ShowAllProjects}" Value="True">
                        <Setter Property="ItemsSource" Value="{Binding Solution.Projects}" />
                      </DataTrigger>
                      <DataTrigger Binding="{Binding SelectedWixProject}" Value="{x:Null}">
                        <Setter Property="IsEnabled" Value="False" />
                      </DataTrigger>
                    </Style.Triggers>
                  </Style>
                </ListBox.Style>
              </ListBox>
            </DockPanel>
            <local:GroupBox.Style>
              <Style TargetType="local:GroupBox">
                <Setter Property="IsOk" Value="True" />
                <Style.Triggers>
                  <DataTrigger Binding="{Binding SelectedItems.Count, ElementName=ProjectsListBox}" Value="0">
                    <Setter Property="IsOk" Value="False" />
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </local:GroupBox.Style>
          </local:GroupBox>
        </Grid>
      </DockPanel>
      <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="5" />
      <Grid Grid.Column="2">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="3*" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <local:GroupBox Grid.Row="0" Ordinal="4" Header="Directory mappings" IsOk="{Binding AreAllDirectoriesMapped}">
          <DataGrid x:Name="DirectoryDataGrid" ItemsSource="{Binding DirectoryMappings}"
                    Style="{StaticResource DirectoryDataGridStyle}">
            <DataGrid.Columns>
              <DataGridTextColumn Header="Directory" Binding="{Binding Directory, StringFormat='.\\{0}'}" Width="200" IsReadOnly="True" />
              <DataGridTemplateColumn Header="WiX definition" Width="*" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate DataType="wax:DirectoryMapping">
                    <Border VerticalAlignment="Center">
                      <Grid>
                        <TextBlock x:Name="resolvedDirectory" DataContext="{Binding MappedNode}">
                          <Run Text="{Binding Id, Mode=OneWay}" />
                          <Span Foreground="Gray">(<Run Text="{Binding Path=Path, Mode=OneWay}" />)</Span>
                        </TextBlock>
                        <ComboBox x:Name="unmappedDirectory" ItemsSource="{Binding UnmappedNodes}"
                                  SelectedItem="{Binding MappedNodeSetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Visibility="Collapsed" />
                      </Grid>
                    </Border>
                    <DataTemplate.Triggers>
                      <DataTrigger Binding="{Binding MappedNode}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Hidden" TargetName="resolvedDirectory" />
                        <Setter Property="Visibility" Value="Visible" TargetName="unmappedDirectory" />
                      </DataTrigger>
                      <DataTrigger Binding="{Binding UnmappedNodes.Count}" Value="0">
                        <Setter Property="Visibility" Value="Collapsed" TargetName="unmappedDirectory" />
                      </DataTrigger>
                    </DataTemplate.Triggers>
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
              <DataGridTemplateColumn IsReadOnly="True" Width="25">
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                    <Grid d:DataContext="{d:DesignInstance wax:DirectoryMapping}">
                      <Button Content="-" Command="{Binding ClearMappingCommand}"
                              Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityConverter}}"
                              Style="{StaticResource SquareControlStyle}" ToolTip="Remove the custom mapping." />
                      <Button Content="+" Command="{Binding AddDirectoryCommand}"
                              Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityConverter}}"
                              Style="{StaticResource SquareControlStyle}" ToolTip="Create a new directory node." />
                    </Grid>
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
            </DataGrid.Columns>
          </DataGrid>
        </local:GroupBox>
        <GridSplitter Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch" Height="5" />
        <local:GroupBox Grid.Row="2" Ordinal="5" Header="File mappings" IsOk="{Binding AreAllFilesMapped}">
          <DataGrid x:Name="FileMappingsGrid" dgx:Tools.ApplyInitialSorting="True"
                    ItemsSource="{Binding FileMappings}"
                    Style="{StaticResource DefaultDataGridStyle}"
                    IsEnabled="{Binding AreAllDirectoriesMapped}">
            <DataGrid.Columns>
              <DataGridTextColumn Header="File" Binding="{Binding UniqueName}" Width="200" IsReadOnly="True" />
              <DataGridTextColumn Header="Ext." Binding="{Binding Extension}" Width="50" IsReadOnly="True" />
              <DataGridTextColumn Header="State" Binding="{Binding MappingState}" Width="100" IsReadOnly="True"
                                  SortDirection="Ascending" />
              <DataGridTemplateColumn Header="WiX definition" Width="*" SortMemberPath="MappedNode.Id"
                                      IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate DataType="wax:FileMapping">
                    <Border VerticalAlignment="Center">
                      <Grid>
                        <TextBlock x:Name="singleFile" DataContext="{Binding MappedNode}">
                          <Run Text="{Binding Id, Mode=OneWay}" />
                          <Span Foreground="Gray">
                            (<Run Text="{Binding Name, Mode=OneWay}" />,
                            <Run Text="{Binding Source, Mode=OneWay}" />)
                          </Span>
                        </TextBlock>
                        <ComboBox x:Name="ambiguousFile" ItemsSource="{Binding UnmappedNodes}"
                                  DisplayMemberPath="Node"
                                  SelectedValuePath="Node"
                                  SelectedValue="{Binding MappedNodeSetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Visibility="Collapsed" />
                      </Grid>
                    </Border>
                    <DataTemplate.Triggers>
                      <DataTrigger Binding="{Binding MappedNode}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Hidden" TargetName="singleFile" />
                        <Setter Property="Visibility" Value="Visible" TargetName="ambiguousFile" />
                      </DataTrigger>
                      <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                          <Condition Binding="{Binding MappedNode}" Value="{x:Null}" />
                          <Condition Binding="{Binding UnmappedNodes.Count}" Value="1" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Visibility" Value="Visible" TargetName="singleFile" />
                        <Setter Property="DataContext" Value="{Binding Path=UnmappedNodes[0].Node}"
                                TargetName="singleFile" />
                        <Setter Property="Visibility" Value="Collapsed" TargetName="ambiguousFile" />
                      </MultiDataTrigger>
                      <DataTrigger Binding="{Binding UnmappedNodes.Count}" Value="0">
                        <Setter Property="Visibility" Value="Collapsed" TargetName="ambiguousFile" />
                      </DataTrigger>
                    </DataTemplate.Triggers>
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
              <DataGridTemplateColumn Width="25">
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                    <Grid d:DataContext="{d:DesignInstance wax:FileMapping}">
                      <Button Content="-" Command="{Binding ClearMappingCommand}"
                              CommandParameter="{Binding Path=SelectedItems, ElementName=FileMappingsGrid}"
                              Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityConverter}}"
                              Style="{StaticResource SquareControlStyle}" ToolTip="Remove custom file mapping." />
                      <Button Content="+" Command="{Binding AddFileCommand}"
                              CommandParameter="{Binding Path=SelectedItems, ElementName=FileMappingsGrid}"
                              Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityConverter}}"
                              Style="{StaticResource SquareControlStyle}" ToolTip="Create a new file node." />
                      <Button Content="?" Command="{Binding ResolveFileCommand}"
                              CommandParameter="{Binding Path=SelectedItems, ElementName=FileMappingsGrid}"
                              Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BooleanToVisibilityConverter}}"
                              Style="{StaticResource SquareControlStyle}" ToolTip="Confirm unique file mapping." />
                    </Grid>
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
              <Style TargetType="DataGridRow">
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Style.Triggers>
                  <DataTrigger Binding="{Binding MappingState}" Value="{x:Static wax:MappingState.Unmapped}">
                    <Setter Property="Background" Value="LightCoral" />
                  </DataTrigger>
                  <DataTrigger Binding="{Binding MappingState}" Value="{x:Static wax:MappingState.Ambiguous}">
                    <Setter Property="Background" Value="DarkOrange" />
                  </DataTrigger>
                  <DataTrigger Binding="{Binding MappingState}" Value="{x:Static wax:MappingState.Unique}">
                    <Setter Property="Background" Value="Yellow" />
                  </DataTrigger>
                </Style.Triggers>
              </Style>
            </DataGrid.RowStyle>
          </DataGrid>
        </local:GroupBox>
        <GridSplitter Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Stretch" Height="5" />
        <GroupBox Grid.Row="4" Header="Unmapped Files">
          <DataGrid ItemsSource="{Binding UnmappedFileNodes}" Style="{StaticResource DefaultDataGridStyle}">
            <DataGrid.Columns>
              <DataGridTextColumn Header="Id" Binding="{Binding Node.Id}" IsReadOnly="True" />
              <DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True" />
              <DataGridTextColumn Header="Source" Binding="{Binding Node.Source, Mode=OneWay}" Width="*" IsReadOnly="True" />
              <DataGridTemplateColumn Width="25">
                <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                    <Grid d:DataContext="{d:DesignInstance wax:UnmappedFile}">
                      <Button Content="-" Command="{Binding DeleteCommand}" Style="{StaticResource SquareControlStyle}"
                              ToolTip="Remove unmapped file." />
                    </Grid>
                  </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
            </DataGrid.Columns>
          </DataGrid>
        </GroupBox>
      </Grid>
    </Grid>
  </DockPanel>
</UserControl>